package com.pachong.android.frameworkbase.imageloader.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.pachong.android.frameworkbase.imageloader.IImageLoader;
import com.pachong.android.frameworkbase.imageloader.ImageLoaderCallback;

/**
 * Universal Image Loader 对图片加载接口的实现类
 * <p/>
 * 注意：需要在Application中初始化
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/6/23 16:59
 * @Version: 1.0.0
 */
public class GlideImageLoader implements IImageLoader {

    private Context mContext;
    private Uri mUri;
    private ImageView mImageView;
    private ImageLoaderCallback mCallback;
    private boolean mSkipMemoryOpen = false;
    private boolean mAsGifOpen = false;
    private boolean mAnimationClose = false;
    private boolean mIfResize=false;
    private int mHeight,mWidth;

    public GlideImageLoader(Context context) {
        mContext = context;
    }


    @Override
    public IImageLoader load(Uri uri) {
        mUri = uri;
        return this;
    }


    /**
     * @param iv
     */
    @Override
    public IImageLoader into(ImageView iv) {
        mImageView = iv;
        return this;
    }


    @Override
    public IImageLoader callback(ImageLoaderCallback cb) {
        mCallback = cb;
        return this;
    }

    @Override
    public IImageLoader skipMemory(boolean bl) {
        mSkipMemoryOpen = bl;
        return this;
    }

    @Override
    public IImageLoader asGif(boolean bl) {
        mAsGifOpen=bl;
        return this;
    }

    @Override
    public IImageLoader resize(boolean bl, int width, int height) {
        mIfResize=true;
        mWidth=width;
        mHeight=height;
        return this;
    }

    @Override
    public void clear() {
        Glide.get(mContext)
                .clearMemory();
    }


    @Override
    public void start() {
        if (mImageView != null) {
            DrawableTypeRequest request = Glide.with(mContext).load(mUri.toString());
            if (mAsGifOpen) {
                request.asGif();
            }
            if(mAnimationClose)
            {
                request.dontAnimate();
            }
            if(mIfResize)
            {
                request.preload(mWidth,mHeight);
            }
            request.skipMemoryCache(mSkipMemoryOpen)
                    .into(mImageView);
            if(mCallback!=null)
            {
                request.into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                        mCallback.onSuccess(bitmap);
                    }
                });
            }
        } else if (mCallback != null) {
            int myWidth = 512;
            int myHeight = 384;
            Glide.with(mContext)
                    .load(mUri.toString())
                    .asBitmap()
                    .into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
                        @Override
                        public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                            Log.d("GlideImageLoader", "Glide start");
                            mCallback.onSuccess(bitmap);
                        }
                    });
        }
    }
}
